package com.nine.demo.filemanager.file;

import com.jcraft.jsch.SftpATTRS;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;

/**
 * @Description:
 * @Author: 刘超
 * @Date: 2023-03-29 17:21
 */
@Setter
@Getter
public class FtpClient {
    //主机地址
    private String host;
    //端口
    private int port=21;
    //ftp用户名称
    private String username;
    //ftp用户密码
    private String password;
    //编码类型
    private String encoding="utf-8";
    private FTPClient client = null;

    public void init(){
        client = new FTPClient();
        client.setControlEncoding(this.encoding);
        try {
            client.connect(this.host,this.port);
            client.login(username,password);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            int replyCode = client.getReplyCode();
            if (FTPReply.isPositiveCompletion(replyCode)) {
                client.disconnect();
            }
        } catch (IOException e) {
            client = null;
        }
    }
    public void destroy(){
        if (client != null) {
            try {
                client.logout();
                client.disconnect();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            client = null;
        }
    }
}
